package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.GuestsCarMapper;
import com.yh.homestay.pojo.GuestsCar;
import com.yh.homestay.service.GuestsCarService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class GuestsCarServiceImpl implements GuestsCarService {

    private Log logger = LogFactory.getLog(GuestsCarServiceImpl.class);

    @Autowired
    private GuestsCarMapper guestsCarMapper;

    /**
    * @Date: 2020/1/13 9:36
    * @Description: 查询出所有车辆信息
    * @param page
     * @param limit
     * @param carType
    * @return: com.yh.homestay.common.HomeStayResult
    */
    @Override
    public HomeStayResult showGuestsCar(Integer page, Integer limit, String carType) {
        try {
            PageHelper.startPage(page, limit);
            List<GuestsCar> guestsCars;
            if (StringUtils.isEmpty(carType)) {
                guestsCars = guestsCarMapper.selectAll();
            } else {
                Example example = new Example(GuestsCar.class);
                example.createCriteria().andLike("carType", "%" + carType + "%");
                guestsCars = guestsCarMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(guestsCars);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
    * @Date: 2020/1/13 9:36
    * @Description: 插入车辆信息
    * @param guestsCar
    * @return: com.yh.homestay.common.GuestsCarResult
    */
    @Override
    public HomeStayResult insertGuestsCar(GuestsCar guestsCar) {
        try{
            guestsCarMapper.insertSelective(guestsCar);
            return HomeStayResult.ok();
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }

    /**
    * @Date: 2020/1/13 9:36
    * @Description: 删除车辆信息
    * @param id
    * @return: com.yh.homestay.common.GuestsCarResult
    */
    @Override
    public HomeStayResult deleteGuestsCar(Integer id) {
        try{
            guestsCarMapper.deleteByPrimaryKey(id);
            return HomeStayResult.ok();
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }

    /**
    * @Date: 2020/1/13 9:37
    * @Description: 根据id查询出一条数据
    * @param id
    * @return: com.yh.homestay.common.GuestsCarResult
    */
    @Override
    public HomeStayResult showOneGuestsCar(Integer id) {
        try{
            GuestsCar guestsCar = guestsCarMapper.selectByPrimaryKey(id);
            return HomeStayResult.ok(200,guestsCar);
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }

    /**
    * @Date: 2020/1/13 9:37
    * @Description: 编辑后更新车辆信息
    * @param guestsCar
    * @return: com.yh.homestay.common.GuestsCarResult
    */
    @Override
    public HomeStayResult updateGuestsCar(GuestsCar guestsCar) {
        try{
            guestsCarMapper.updateByPrimaryKey(guestsCar);
            return HomeStayResult.ok();
        }catch (Exception e){
            return HomeStayResult.ok();
        }
    }

    /**
    * @Date: 2020/1/13 9:37
    * @Description: 批量删除车辆信息
    * @param ids
    * @return: com.yh.homestay.common.GuestsCarResult
    */
    @Override
    public HomeStayResult deleteBatchGuestsCar(String ids) {
        try {
            String[] id = ids.split(",");
            for (int i = 0; i < id.length; i++) {
                guestsCarMapper.deleteByPrimaryKey(id[i]);
            }
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return HomeStayResult.build(500, "error");
        }
    }
}